package com.groot.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;


/**
 * @decoration 统一资源拦截器
 * classpath:xin.spring.medical.common.filter.ResourcesFilter.java
 * @author spring
 * @date 2019-02-27 10:17:00
 */
@WebFilter(filterName="ResourcesFilter",urlPatterns="/*",
		initParams={
				@WebInitParam(name="statics",value="/static"),
				@WebInitParam(name="loginURI",value="/login.do"),
				@WebInitParam(name="medical",value="/midecal"),
				@WebInitParam(name="file",value="/file.do"),
				@WebInitParam(name="user",value="/user.do"),
				@WebInitParam(name="role",value="/role.do"),
				@WebInitParam(name="menu",value="/menu.do"),
				@WebInitParam(name="medicalI",value="/medicalI.do"),
				@WebInitParam(name="area",value="/area.do"),
				@WebInitParam(name="institution",value="/institution.do"),
				@WebInitParam(name="illtype",value="/illtype.do"),
				@WebInitParam(name="family",value="/family.do"),
				@WebInitParam(name="person",value="/person.do"),
				@WebInitParam(name="pay",value="/pay.do"),
				@WebInitParam(name="paysetting",value="/paysetting.do"),
				@WebInitParam(name="pattern",value="/pattern.do"),
				@WebInitParam(name="policy",value="/policy.do"),
				@WebInitParam(name="expense",value="/expense.do"),
			    @WebInitParam(name="examine",value="/examine.do"),
		})
public class ResourcesFilter implements Filter {

	/**
	 * 静态资源放行
	 */
	private String statics;
	
	/**
	 * 登陆页面放行
	 */
	private String loginURI;
	
	/**
	 * 默认页面放行
	 */
	private String medical;
	
	/**
	 * 文件上传放行
	 */
	private String file;
	/**
	 * 用户信息
	 */
	private String user;
	/**
	 * 角色信息
	 */
	private String role;
	/**
	 * 权限信息
	 */
	private  String menu;
	
	private String medicalI;
	
	private String area;
	private String institution;
	private String illtype;
	private String family;
	private String person;
	private String pay;
	private String paysetting;
	private String pattern;
	private String policy;
	private String expense;
	private String examine;
	/**
	 * Log4j
	 */
	private static Logger LOGGER = Logger.getLogger(ResourcesFilter.class);
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		statics = filterConfig.getInitParameter("statics");
		loginURI = filterConfig.getInitParameter("loginURI");
		medical = filterConfig.getInitParameter("medical");
		file = filterConfig.getInitParameter("file");
		user = filterConfig.getInitParameter("user");
		role = filterConfig.getInitParameter("role");
		menu = filterConfig.getInitParameter("menu");
		medicalI = filterConfig.getInitParameter("medicalI");
		area = filterConfig.getInitParameter("area");
		institution = filterConfig.getInitParameter("institution");
		illtype = filterConfig.getInitParameter("illtype");
		family = filterConfig.getInitParameter("family");
		person = filterConfig.getInitParameter("person");
		pay = filterConfig.getInitParameter("pay");
		paysetting = filterConfig.getInitParameter("paysetting");
		pattern = filterConfig.getInitParameter("pattern");
		policy = filterConfig.getInitParameter("policy");
		expense = filterConfig.getInitParameter("expense");
		examine = filterConfig.getInitParameter("examine");
		LOGGER.info("..ResourcesFilter.." + statics);
		LOGGER.info("..ResourcesFilter.." + loginURI);
		LOGGER.info("..ResourcesFilter.." + medical);
		LOGGER.info("..ResourcesFilter.." + file);
		LOGGER.info("..ResourcesFilter.." + user);
		LOGGER.info("..ResourcesFilter.." + role);
		LOGGER.info("..ResourcesFilter.." + menu);
		LOGGER.info("..ResourcesFilter.." + medicalI);
		LOGGER.info("..ResourcesFilter.." + area);
		LOGGER.info("..ResourcesFilter.." + institution);
		LOGGER.info("..ResourcesFilter.." + illtype);
		LOGGER.info("..ResourcesFilter.." + family);
		LOGGER.info("..ResourcesFilter.." + person);
		LOGGER.info("..ResourcesFilter.." + pay);
		LOGGER.info("..ResourcesFilter.." + paysetting);
		LOGGER.info("..ResourcesFilter.." + pattern);
		LOGGER.info("..ResourcesFilter.." + policy);
		LOGGER.info("..ResourcesFilter.." + expense);
		LOGGER.info("..ResourcesFilter.." + examine);
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		String requestURI = req.getRequestURI();
		LOGGER.info("req.getRequestURI():" + requestURI);
		if(requestURI.indexOf(statics) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(loginURI) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(file) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(user) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(role) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(menu) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(medicalI) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(area) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(institution) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(illtype) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(family) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(person) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(pay) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(paysetting) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(pattern) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(policy) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(expense) != -1){
			chain.doFilter(request, response);
			return;
		}
		if(requestURI.indexOf(examine) != -1){
			chain.doFilter(request, response);
			return;
		}
		String user = (String)req.getSession().getAttribute("user");
		if(user != null && !user.isEmpty()){
			chain.doFilter(request, response);
			return;
		}
		if(medical.equals(requestURI) || (medical+"/").equals(requestURI)){
			req.getRequestDispatcher("/login.do?method=login").forward(request, response);
			return;
		}
		req.getRequestDispatcher("/index.do?method=to404").forward(request, response);
	}

	@Override
	public void destroy() {
		LOGGER.info("ResourcesFilter is destroy()");
	}

}
